package xin.marcher.module.price.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xin.marcher.module.common.enums.inventory.InventoryExceptionCode;
import xin.marcher.module.common.enums.price.PriceTypeEnum;
import xin.marcher.module.common.exception.BaseBizException;
import xin.marcher.module.price.domain.dto.SkuPriceSellerRelationDTO;
import xin.marcher.module.price.domain.dto.SkuPriceSellerRelationResultDTO;
import xin.marcher.module.price.domain.request.SkuPriceRequest;
import xin.marcher.module.price.repository.SkuPriceRepository;
import xin.marcher.module.price.service.SkuPriceSellerRelationService;

import java.util.List;
import java.util.Objects;

/**
 *
 */
@Service
public class SkuPriceSellerRelationServiceImpl implements SkuPriceSellerRelationService {

    @Autowired
    private SkuPriceRepository skuPriceRepository;

    /**
     * 分卖家定价
     *
     * @param request
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public SkuPriceSellerRelationResultDTO pricingBySeller(SkuPriceRequest request) {
        // 入参校验
        checkSaveParams(request);
        // 填充卖家类型
        skuPriceRepository.fillSellerType(request);
        // 卖家定价保存到草稿
        skuPriceRepository.savePriceToDraft(request);
        return new SkuPriceSellerRelationResultDTO(Boolean.TRUE);
    }

    /**
     * 更新商品价格
     *
     * @param request
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public SkuPriceSellerRelationResultDTO updatePrice(SkuPriceRequest request) {
        // 入参校验
        checkUpdateParams(request);
        // 价格数据是否存在
        request = skuPriceRepository.checkSkuPrice(request);
        // 填充卖家类型
        skuPriceRepository.fillSellerType(request);
        // 累加版本号
        skuPriceRepository.incrVersionId(request);
        // 商品价格保存到草稿
        skuPriceRepository.updatePriceToDraft(request);
        return new SkuPriceSellerRelationResultDTO(Boolean.TRUE);
    }

    /**
     * 查询商品在某个卖家的定价
     * skuId、sellerId可以确定一件商品的价格，分为商城价和会员价等价格
     *
     * @param request
     * @return
     */
    @Override
    public List<SkuPriceSellerRelationDTO> querySkuPrice(SkuPriceRequest request) {
        // 入参校验，如果skuId 或 sellerId没有，就禁止查询
        checkQueryParams(request);
        return skuPriceRepository.querySkuPrice(request);
    }

    /**
     * 分卖家定价入参校验
     *
     * @param request
     */
    private void checkSaveParams(SkuPriceRequest request) {
        if (Objects.isNull(request)) {
            throw new BaseBizException(InventoryExceptionCode.PARAM_CHECK_ERROR, InventoryExceptionCode.PARAM_CHECK_ERROR.getErrorCode());
        }

        if (StringUtils.isEmpty(request.getSkuId())) {
            throw new BaseBizException(InventoryExceptionCode.PARAM_CHECK_ERROR, InventoryExceptionCode.PARAM_CHECK_ERROR.getErrorCode());
        }

        if (Objects.isNull(request.getSellerId())) {
            throw new BaseBizException(InventoryExceptionCode.PARAM_CHECK_ERROR, InventoryExceptionCode.PARAM_CHECK_ERROR.getErrorCode());
        }

        if (Objects.isNull(PriceTypeEnum.getByCode(request.getPriceType()))) {
            throw new BaseBizException(InventoryExceptionCode.PARAM_CHECK_ERROR, InventoryExceptionCode.PARAM_CHECK_ERROR.getErrorCode());
        }

        if (Objects.isNull(request.getPrice())) {
            throw new BaseBizException(InventoryExceptionCode.PARAM_CHECK_ERROR, InventoryExceptionCode.PARAM_CHECK_ERROR.getErrorCode());
        }

        if (Objects.isNull(request.getSuggestedMallPrice())) {
            throw new BaseBizException(InventoryExceptionCode.PARAM_CHECK_ERROR, InventoryExceptionCode.PARAM_CHECK_ERROR.getErrorCode());
        }
    }

    /**
     * 查询商品价格入参校验
     *
     * @param request
     */
    private void checkQueryParams(SkuPriceRequest request) {
        if (Objects.isNull(request)) {
            throw new BaseBizException(InventoryExceptionCode.PARAM_CHECK_ERROR, InventoryExceptionCode.PARAM_CHECK_ERROR.getErrorCode());
        }

        if (StringUtils.isEmpty(request.getSkuId())) {
            throw new BaseBizException(InventoryExceptionCode.PARAM_CHECK_ERROR, InventoryExceptionCode.PARAM_CHECK_ERROR.getErrorCode());
        }

        if (Objects.isNull(request.getSellerId())) {
            throw new BaseBizException(InventoryExceptionCode.PARAM_CHECK_ERROR, InventoryExceptionCode.PARAM_CHECK_ERROR.getErrorCode());
        }
    }

    /**
     * 修改商品价格入参检查
     *
     * @param request
     */
    private void checkUpdateParams(SkuPriceRequest request) {
        // 限定条件，根据skuId、sellerId、priceType，可以唯一确定一条价格记录
        if (Objects.isNull(request)) {
            throw new BaseBizException(InventoryExceptionCode.PARAM_CHECK_ERROR, InventoryExceptionCode.PARAM_CHECK_ERROR.getErrorCode());
        }

        if (StringUtils.isEmpty(request.getSkuId())) {
            throw new BaseBizException(InventoryExceptionCode.PARAM_CHECK_ERROR, InventoryExceptionCode.PARAM_CHECK_ERROR.getErrorCode());
        }

        if (Objects.isNull(request.getSellerId())) {
            throw new BaseBizException(InventoryExceptionCode.PARAM_CHECK_ERROR, InventoryExceptionCode.PARAM_CHECK_ERROR.getErrorCode());
        }

        if (Objects.isNull(PriceTypeEnum.getByCode(request.getPriceType()))) {
            throw new BaseBizException(InventoryExceptionCode.PARAM_CHECK_ERROR, InventoryExceptionCode.PARAM_CHECK_ERROR.getErrorCode());
        }
    }

}
